package com.mi.rule.db.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.mi.rule.db.entity.MiEntity;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

/**
 * 帮助类
 *
 * @author 王永吉
 */
@Data
public class MiHelp {

    public static <P extends MiEntity> void add(P entity) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
        if (tableInfo == null) TableInfoHelper.initTableInfo(null, entity.getClass());
    }

    public static <P extends MiEntity> void add(Class<P> tClass) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(tClass);
        if (tableInfo == null) TableInfoHelper.initTableInfo(null, tClass);
    }

    public static <P extends MiEntity> String getTableName(Class<P> tClass) {
        add(tClass);
        return TableInfoHelper.getTableInfo(tClass).getTableName();
    }

    public static <P extends MiEntity> String getTableName(P entity) {
        add(entity);
        return TableInfoHelper.getTableInfo(entity.getClass()).getTableName();
    }

    public static <P extends MiEntity> String getTableId(Class<P> tClass) {
        add(tClass);
        return TableInfoHelper.getTableInfo(tClass).getKeyColumn();
    }

    public static <P extends MiEntity> String getTableId(P entity) {
        add(entity.getClass());
        return TableInfoHelper.getTableInfo(entity.getClass()).getKeyColumn();
    }

    /**
     * 非空字段
     *
     * @return <非空属性，column>
     */
    public static HashMap<String, Object> getNotNullColumn(Object entity) {
        HashMap<String, Object> columnMap = new HashMap<>();
        Class<?> mClass = entity.getClass();
        Field[] declaredFields = mClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            TableId tableId = declaredField.getAnnotation(TableId.class);
            TableField tableField = declaredField.getAnnotation(TableField.class);
            String column = "";

            //空属性不存储
            if (tableField == null && tableId == null) continue;
            if (tableField != null && !tableField.exist()) continue;
            declaredField.setAccessible(true);

            //updateEntity <非空属性字段，值>
            if (tableId != null) column = tableId.value();
            if (tableField != null) column = tableField.value();
            try {
                Object tableValue = declaredField.get(entity);
                if (tableValue != null) {
                    columnMap.put(declaredField.getName(), column);
                }
            } catch (IllegalAccessException ignored) {
            }
        }
        return columnMap;
    }

    public static <P extends MiEntity> Object getIdValue(P entity) {
        String idValue = null;
        Class<?> mClass = entity.getClass();
        Field[] declaredFields = mClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            TableId tableId = declaredField.getAnnotation(TableId.class);
            //设置ID
            if (tableId != null) {
                declaredField.setAccessible(true);
                try {
                    idValue = declaredField.get(entity).toString();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return idValue;
    }


    public static <T> T parsObj(Object map, Class<T> tClass) {
        if (map == null) return null;
        String s = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        return JSONObject.parseObject(s, tClass);
    }

    public static <T> List<T> parsArray(Object map, Class<T> tClass) {
        if (map == null) return null;
        String s = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        return JSONObject.parseArray(s, tClass);
    }

    public static <P extends MiEntity> String getSqlSelect(MiWrapper<P> wrapper) {
        if (wrapper.getSqlSelect() == null || wrapper.getSqlSelect().equals("")) {
            wrapper.select("*");
        }
        return wrapper.getSqlSelect();
    }


}
